package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptionResolver.ExceptionMessage;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    PermissionService permissionService;
    @RequestMapping("/add")
    @ExceptionMessage(value = MessageConstant.ADD_PERMISSION_FALL)
    public Result add(@RequestBody Permission permission){
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 可以批量删除，后面同理
     * @param ids
     * @return
     */
    @RequestMapping("/del")
    @ExceptionMessage(value = MessageConstant.DEL_PERMISSION_FALL)
    public Result del(Integer[] ids){
        if (ids.length==0||ids==null){
            return new Result(false,MessageConstant.DEL_PERMISSION_MSG );
        }
            permissionService.del(ids);
            return new Result(true, MessageConstant.DEL_PERMISSION_SUCCESS);
    }

    @RequestMapping("/update")
    @ExceptionMessage(value = MessageConstant.EDIT_PERMISSION_FALL)
    public Result update(@RequestBody Permission permission){
            permissionService.update(permission);
            return new Result(true,MessageConstant.EDIT_PERMISSION_SUCCESS );
    }

    @RequestMapping("/find")
    @ExceptionMessage(value = MessageConstant.GET_PERMISSION_FALL)
    public Result findById(Integer id){
            Permission permission=permissionService.find(id);
            return new Result(true,MessageConstant.GET_PERMISSION_SUCCESS ,permission);
    }

    @RequestMapping("/findPage")
    @ExceptionMessage(value = MessageConstant.GET_PERMISSION_FALL)
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= null;
            pageResult = permissionService.findPage(queryPageBean);
            return new Result(true,MessageConstant.GET_PERMISSION_SUCCESS,pageResult);
    }

    @RequestMapping("/findAll")
    @ExceptionMessage(value = MessageConstant.GET_PERMISSION_FALL)
    public Result findAll(){
            List<Permission> permissions = permissionService.findAll();
            return new Result(true,MessageConstant.GET_PERMISSION_SUCCESS,permissions);
    }
}
